<?php
class Pelapor_model  extends MY_Model {
	
    function Pelapor_model(){
        parent::MY_Model();
        $this->load->database();
        $this->table_name = "pelapor";
        $this->table_fields = array("lapor_id","lapor_nik","lapor_nama");
    }
    
    function select_last_entry($search_key=false, $where=false, $limit=10, $offset=0, $orderby=""){
		$db = $this->db;
    	$db->select('*');
		//$db->join('pelapor_alamat', 'pelapor_alamat.lapor_id = '.$this->table_name.'.lapor_id');
		//$db->join('pelapor_telepon', 'pelapor_telepon.lapor_id = '.$this->table_name.'.lapor_id');
		
		if ($search_key){
			if (!is_array($search_key)) $search_key = array($search_key);
			
			$qq = '';
			foreach($search_key as $kk){
				foreach($this->table_fields as $ff){
					$qq .= ($qq? " OR " : "").$this->table_name.".$ff LIKE ". $this->db->escape("%$kk%"); 
				}
				
				//join table
//				$join_fields = array(
//					'alamat'=>array('lapor_alm_id','lapor_alm_alamat','lapor_alm_kode_pos',
//						'lapor_alm_kode_area','lapor_alm_kota','lapor_alm_propinsi'),
//					'tlp'=>array('lapor_tlp_no')
//				);
//				foreach ($join_fields as $jj){
//					foreach($jj as $ff){
//						$qq .= ($qq? " OR " : "")."$ff LIKE ". $this->db->escape("%$kk%"); 
//					}
//				}
			}
			$db->where("($qq)");
		}
		
		if ($where) $db->where($where);
		
    	if ($orderby) $db->order_by($orderby);
		
		$db->limit($limit, $offset);
		
		return $db->get($this->table_name);
    }
    
    function select_by_nik($nik){
    	return $this->get_where(array('lapor_nik' => $nik));
    }
        
    function select_by_id($id){
    	return $query = $this->db->get_where($this->table_name, array('lapor_id' => $id));
    }
    
    function get_last_id(){
    	$this->db->select_max($this->table_fields[0]);
    	$rr = $this->db->get($this->table_name);
    	$rr = (array)$rr->row();
    	return $rr[$this->table_fields[0]];
    }
}